package com.visionet.school.app.web.web;

import com.visionet.school.app.web.common.utils.PageInfo;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.entrust.vo.EntrustVo;
import com.visionet.school.app.web.entity.enums.EntrustEnums;
import com.visionet.school.app.web.entity.enums.MemberType;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.entity.vo.StudentVo;
import com.visionet.school.app.web.service.entrust.EntrustService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/2.
 */
@Controller
@RequestMapping("/entrust")
public class EntrustController {

    @Autowired
    private EntrustService entrustService;


    /**
     * 保存嘱托
     * @param entrustVo
     * @return
     */
    @RequestMapping(value = "/saveEntrust", method = RequestMethod.POST)
    public ResponseEntity<?> saveEntrust(@RequestBody EntrustVo entrustVo){
        String entrustType = entrustVo.getEntrustType();

        if(entrustVo == null || StringUtils.isBlank(entrustType) || entrustVo.getStudentIds() == null || entrustVo.getStudentIds().size() < 1){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        if(entrustType.equals(EntrustEnums.AUDIO.toKey()) && StringUtils.isBlank(entrustVo.getMediaId())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        if(entrustType.equals(EntrustEnums.TEXT.toKey()) && StringUtils.isBlank(entrustVo.getEntrustContent())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            entrustService.save(entrustVo);
            map.put("stat","success");
        }catch (Exception e){
            map.put("stat","err");
            map.put("msg",e.getMessage());
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    /**
     * 确认执行嘱托
     * @param entrustId
     * @return
     */
    @RequestMapping(value = "/executedEntrust", method = RequestMethod.GET)
    public ResponseEntity<?> executedEntrust(String entrustId){
        if(StringUtils.isBlank(entrustId)){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            entrustService.updateExecutedEntrust(entrustId);
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
            map.put("msg",e.getMessage());
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    /**
     * wechat   家长/教师 获取嘱托列表
     * @param entrustVo
     * @return
     */
    @RequestMapping(value = "/getEntrustPage", method = RequestMethod.POST)
    public ResponseEntity<?> getEntrustPage(@RequestBody EntrustVo entrustVo){
        if(entrustVo == null){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        if(entrustVo.getPageInfo() == null){
            entrustVo.setPageInfo(new PageInfo());
        }

        LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();

//        List<StudentVo> studentVoList = new ArrayList<>();
//        StudentVo studentVo1 = new StudentVo();
//        studentVo1.setMemberId("041DFD58D85D31A478B881F1EBE4ECA9");
//        studentVoList.add(studentVo1);
//        StudentVo studentVo2 = new StudentVo();
//        studentVo2.setMemberId("041DFD58D85D31A478B881F1EBE4ECA9");
//        studentVoList.add(studentVo1);

        if(loginAccountVo.getMemberType().equals(MemberType.PARENT.toKey())){
            List<StudentVo> studentVoList = loginAccountVo.getStudentVoList();
            if(studentVoList != null){
                List<String> studentIds = new ArrayList<>();
                for(StudentVo studentVo : studentVoList){
                    studentIds.add(studentVo.getMemberId());
                }
                entrustVo.setStudentIds(studentIds);
            }else{
                return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
            }
        }else if(loginAccountVo.getMemberType().equals(MemberType.TEACHER.toKey())){
            entrustVo.setToEntrustId(loginAccountVo.getMemberId());
        }else{
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }


        Map<String,Object> map = new HashMap<>();
        Map<String,Object> pageResult = new HashMap<>();
        try {
            if(StringUtils.isNotBlank(entrustVo.getSortType())){
                if(entrustVo.getSortType().equals("executeTime")){
                    pageResult = entrustService.getEntrustPage(entrustVo,"executeTime");
                }else{
                    pageResult = entrustService.getEntrustPage(entrustVo,"entrustTime");
                }
            }else{
                pageResult = entrustService.getEntrustPage(entrustVo,"entrustTime");
            }
            map.put("pageResult",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/getEntrustInfo" ,method = RequestMethod.GET)
    public ResponseEntity<?> getEntrustInfo(String entrustId){
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isBlank(entrustId)){
            return new ResponseEntity(map , HttpStatus.NOT_ACCEPTABLE);
        }
        map.put("entrust",entrustService.getEntrustInfo(entrustId));
        return new ResponseEntity(map , HttpStatus.OK);
    }






}
